#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int main()
{
    //创建文件并写入数据
    int fd = open("bite.c", O_WRONLY | O_CREAT, 0644);
    const char *data = "i like linux!\n";
    ssize_t result = write(fd, data, strlen(data));
    if(result == -1){
        perror("error write\n");
        close(fd);
        return 1;
    }

    close(fd);

    //读取文件内容
    fd = open("bite.c",O_RDONLY);
    if(fd == -1){
        perror("error open!\n");
        return 1;
    }
    char buffer[100];
    result = read(fd, buffer, sizeof(buffer));
    if(result == -1){
        perror("error read\n");
        close(fd);
        return 1;
    }

    printf("%s",buffer);
    close(fd);

    //移动光标
    fd = open("bite.c", O_RDWR);
    if(fd == -1){
        perror("error open\n");
        return 1;
    }
    off_t i = 0;
    if(lseek(fd, i, SEEK_SET) == (off_t)-1){
        perror("error lseek\n");
        close(fd);
        return 1;
    }
    fd = open("bite.c",O_RDONLY);
    if(fd == -1){
        perror("error open\n");
        return 1;
    }
    char buffer2[100];
    result = read(fd, buffer2,sizeof(buffer2));
    if(result == -1){
        perror("error read\n");
        close(fd);
        return 1;
    }
    buffer2[result] = '\0';
    printf("%s",buffer2);
    close(fd);

    return 0;
}
